package com.sisqorap.registrations;

import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

//http://fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx

public class RegistrationsActivity extends Activity {
	
	private final static String SERVICE_URI = "http://10.0.2.2:52878/ClientBin/SLOData-Web-Services-EmployeesDomainService.svc";
	
	
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.main); 
	}
	
	@Override
	public void onResume(){
		super.onResume();
		refreshData();
	}
	
	private void refreshData(){
		try{
			HttpGet request = new HttpGet(SERVICE_URI + "/OData/");
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");
			
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);          
			HttpEntity responseEntity = response.getEntity(); 
			
			// Read response data into buffer
			char[] buffer = new char[(int)responseEntity.getContentLength()];         
			InputStream stream = responseEntity.getContent();         
			InputStreamReader reader = new InputStreamReader(stream);         
			reader.read(buffer);
			stream.close();
			//JSONArray data = new JSONArray("["+new String(buffer)+"]");
			//data.getString(0);
			TextView tv = (TextView)findViewById(R.id.tvData);
			tv.setText(new String(buffer));
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}